<template>
</template>

<script>
	import {
		mapState
	} from 'pinia';
	import {
		useLoginUserStore
	} from '@/stores/login-user.js';
	const dd = require('@/static/js/dingtalk.open.js');
	export default {
		data(){
			return {
				
			}
		},
		computed: {
			...mapState(useLoginUserStore, ['userId'])
		},
		methods: {
			ddScanCode(){
				let _that = this
				if(dd.env.platform !== 'notInDingTalk'){
					//钉钉端
					dd.ready(function() {
					    dd.biz.util.scan({
					        type: 'qrCode' ,
					        onSuccess: function(res) {
								let data = JSON.parse(res.text)
								if(data.intervalTime){//动态二维码
									let yxdate = data.date + data.intervalTime
									let dqdate = new Date().getTime()
									if(yxdate>dqdate){
										uni.setStorageSync('scanCodeResultCache',res.text)
										uni.navigateTo({
										    url: '/pages/scan/scanResult/scanResult'
										})
									}else{
										_that.om.toast('二维码已失效，请重新获取二维码！')
									}
								}else{//静态二维码
									uni.setStorageSync('scanCodeResultCache',res.text)
									uni.navigateTo({
									    url: '/pages/scan/scanResult/scanResult'
									})
								}
					        },
					        onFail : function(err) {
								_that.om.toast(err)
					        }
					    })
					});
				}
				else if(window.navigator.userAgent.toLowerCase().match(/MicroMessenger/i) == 'micromessenger'){
					//微信端
					uni.showModal({
						title: '提示',
						content: '请在钉钉内置容器中使用！',
						showCancel: false,
						success(res){
							if(res.confirm){
								uni.navigateBack({
								    url: '/pages/main/main/main'
								})
							}
						}
					})
					// wx.ready(function() {
					//     wx.checkJsApi({
					//         jsApiList: ["scanQRCode"],
					//         success: function(res) {
					//             wx.scanQRCode({
					// 				onlyFromCamera: true,
					// 				scanType: ["qrCode"],
					// 				success: function(res) {
					// 					let obj = JSON.parse(res.result)
					// 					obj.userId = _that.userId;//签到人员
					// 					obj.signDate = new Date().getTime();//签到时间戳
					// 					_that.om.request(obj.url.substring(1,obj.url.length-3), obj, 'post').then(res => {
					// 						if(res.succeed == true){
					// 							_that.om.toast(res.value)
					// 						}else{
					// 							_that.om.toast(res.msg)
					// 						}
					// 					})
					// 				},
					// 				error: function(err) {
					// 					_that.om.toast('扫描失败:扫描码=' + err);
					// 				}
					//             });
					//         }
					//     });
					// });
				}else{
					uni.showModal({
						title: '提示',
						content: '请在钉钉内置容器中使用！',
						showCancel: false,
						success(res){
							if(res.confirm){
								uni.navigateBack({
								    url: '/pages/main/main/main'
								})
							}
						}
					})
				}
			}
		},
		onLoad(){
			this.ddScanCode()
		}
	}
</script>

<style>
</style>
